﻿using System;
using System.Drawing;
using System.Linq;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using SenseTimeTracker.Misc;
using StedySoft.SenseSDK;

namespace SenseTimeTracker.CommonSenseControls
{
	public class SensePanelDateTimeItemEx : SenseListControl.ISenseListItem
	{
		private SensePanelDateTimeItem dateTimeItem;
		private SensePanelTextboxItem textBoxItem;
		private SensePanelDateTimeItem.Style displayStyle = SensePanelDateTimeItem.Style.DateTime;

		// Declare the delegate (if using non-generic pattern).
		public delegate void DateTimeChangedEventHandler(object sender, DateTime dateTime);

		// Declare the event.
		public event DateTimeChangedEventHandler OnDateTimeChanged;

		public SensePanelDateTimeItemEx()
		{
			if (Persistance.Instance.UseTextualDateTime)
			{
				textBoxItem = new SensePanelTextboxItem();
				textBoxItem.TextChanged += SenseTextDateTimePanelItem_TextChanged;
			}
			else
			{
				dateTimeItem = new SensePanelDateTimeItem();
				dateTimeItem.OnDateTimeChanged += dateTimeItem_OnDateTimeChanged;
			}
		}

		void dateTimeItem_OnDateTimeChanged(object Sender, DateTime dateTime)
		{
			if (OnDateTimeChanged != null)
			{
				OnDateTimeChanged(this, dateTime);
			}
		}

		void SenseTextDateTimePanelItem_TextChanged(object sender, EventArgs e)
		{
			try
			{
				DateTime dateTime = DateTime.Parse(textBoxItem.Text);
				if (OnDateTimeChanged != null)
				{
					OnDateTimeChanged(this, dateTime);
				}
			}
			catch
			{
			}
		}

		[SuppressMessage("Microsoft.StyleCop.CSharp.LayoutRules", "SA1503:CurlyBracketsMustNotBeOmitted", Justification = "can contain serialize")]
		public SensePanelDateTimeItem.Style DisplayStyle
		{
			get
			{
				if (textBoxItem != null) return displayStyle;
				if (dateTimeItem != null) return dateTimeItem.DisplayStyle;
				return SensePanelDateTimeItem.Style.DateTime;
			}

			set
			{
				if (textBoxItem != null) displayStyle = value;
				if (dateTimeItem != null) dateTimeItem.DisplayStyle = value;
			}
		}

		[SuppressMessage("Microsoft.StyleCop.CSharp.LayoutRules", "SA1503:CurlyBracketsMustNotBeOmitted", Justification = "can contain serialize")]
		public string LabelText
		{
			get
			{
				if (textBoxItem != null) return textBoxItem.LabelText;
				if (dateTimeItem != null) return dateTimeItem.LabelText;
				return string.Empty;
			}
			set
			{
				if (textBoxItem != null) textBoxItem.LabelText = value;
				if (dateTimeItem != null) dateTimeItem.LabelText = value;
			}
		}

		[SuppressMessage("Microsoft.StyleCop.CSharp.LayoutRules", "SA1503:CurlyBracketsMustNotBeOmitted", Justification = "can contain serialize")]
		public bool ShowSeparator
		{
			get
			{
				if (textBoxItem != null) return textBoxItem.ShowSeparator;
				if (dateTimeItem != null) return dateTimeItem.ShowSeparator;
				return false;
			}
			set
			{
				if (textBoxItem != null) textBoxItem.ShowSeparator = value;
				if (dateTimeItem != null) dateTimeItem.ShowSeparator = value;
			}
		}

		[SuppressMessage("Microsoft.StyleCop.CSharp.LayoutRules", "SA1503:CurlyBracketsMustNotBeOmitted", Justification = "can contain serialize")]
		public DateTime DateTime
		{
			get
			{
				if (textBoxItem != null)
				{
					try
					{
						return DateTime.Parse(textBoxItem.Text);
					}
					catch
					{
						return DateTime.MinValue;
					}
				}

				if (dateTimeItem != null)
				{
					return dateTimeItem.DateTime;
				}

				return DateTime.MinValue;
			}

			set
			{
				if (textBoxItem != null)
				{
					switch (displayStyle)
					{
						case SensePanelDateTimeItem.Style.Date:
							textBoxItem.Text = value.Date.ToString();
							break;
						case SensePanelDateTimeItem.Style.Time:
							textBoxItem.Text = value.TimeOfDay.ToString();
							break;
						case SensePanelDateTimeItem.Style.DateTime:
							textBoxItem.Text = value.ToString();
							break;
					}
				}
				if (dateTimeItem != null) dateTimeItem.DateTime = value;
			}
		}

		#region ISenseListItem Members

		public List<SenseListControl.ISenseListItem> Children
		{
			get { return null; }
			set { }
		}

		[SuppressMessage("Microsoft.StyleCop.CSharp.LayoutRules", "SA1503:CurlyBracketsMustNotBeOmitted", Justification = "can contain serialize")]
		public Rectangle ClientRectangle
		{
			get
			{
				if (textBoxItem != null) return textBoxItem.ClientRectangle;
				if (dateTimeItem != null) return dateTimeItem.ClientRectangle;
				return Rectangle.Empty;
			}
			set
			{
				if (textBoxItem != null) textBoxItem.ClientRectangle = value;
				if (dateTimeItem != null) dateTimeItem.ClientRectangle = value;
			}
		}

		[SuppressMessage("Microsoft.StyleCop.CSharp.LayoutRules", "SA1503:CurlyBracketsMustNotBeOmitted", Justification = "can contain serialize")]
		public bool Enabled
		{
			get
			{
				if (textBoxItem != null) return textBoxItem.Enabled;
				if (dateTimeItem != null) return dateTimeItem.Enabled;
				return false;
			}
			set
			{
				if (textBoxItem != null) textBoxItem.Enabled = value;
				if (dateTimeItem != null) dateTimeItem.Enabled = value;
			}
		}

		[SuppressMessage("Microsoft.StyleCop.CSharp.LayoutRules", "SA1503:CurlyBracketsMustNotBeOmitted", Justification = "can contain serialize")]
		public int Height
		{
			get
			{
				if (textBoxItem != null) return textBoxItem.Height;
				if (dateTimeItem != null) return dateTimeItem.Height;
				return 0;
			}
			set
			{
				if (textBoxItem != null) textBoxItem.Height = value;
				if (dateTimeItem != null) dateTimeItem.Height = value;
			}
		}

		[SuppressMessage("Microsoft.StyleCop.CSharp.LayoutRules", "SA1503:CurlyBracketsMustNotBeOmitted", Justification = "can contain serialize")]
		public int Index
		{
			get
			{
				if (textBoxItem != null) return textBoxItem.Index;
				if (dateTimeItem != null) return dateTimeItem.Index;
				return 0;
			}
			set
			{
				if (textBoxItem != null) textBoxItem.Index = value;
				if (dateTimeItem != null) dateTimeItem.Index = value;
			}
		}

		[SuppressMessage("Microsoft.StyleCop.CSharp.LayoutRules", "SA1503:CurlyBracketsMustNotBeOmitted", Justification = "can contain serialize")]
		public string Name
		{
			get
			{
				if (textBoxItem != null) return textBoxItem.Name;
				if (dateTimeItem != null) return dateTimeItem.Name;
				return string.Empty;
			}
			set
			{
				if (textBoxItem != null) textBoxItem.Name = value;
				if (dateTimeItem != null) dateTimeItem.Name = value;
			}
		}

		[SuppressMessage("Microsoft.StyleCop.CSharp.LayoutRules", "SA1503:CurlyBracketsMustNotBeOmitted", Justification = "can contain serialize")]
		public void OnGotFocus(int x, int y)
		{
			if (textBoxItem != null) textBoxItem.OnGotFocus(x, y);
			if (dateTimeItem != null) dateTimeItem.OnGotFocus(x, y);
		}

		[SuppressMessage("Microsoft.StyleCop.CSharp.LayoutRules", "SA1503:CurlyBracketsMustNotBeOmitted", Justification = "can contain serialize")]
		public void OnHandleCreated()
		{
			if (textBoxItem != null) textBoxItem.OnHandleCreated();
			if (dateTimeItem != null) dateTimeItem.OnHandleCreated();
		}

		[SuppressMessage("Microsoft.StyleCop.CSharp.LayoutRules", "SA1503:CurlyBracketsMustNotBeOmitted", Justification = "can contain serialize")]
		public void OnHandleDisposed()
		{
			if (textBoxItem != null) textBoxItem.OnHandleDisposed();
			if (dateTimeItem != null) dateTimeItem.OnHandleDisposed();
		}

		[SuppressMessage("Microsoft.StyleCop.CSharp.LayoutRules", "SA1503:CurlyBracketsMustNotBeOmitted", Justification = "can contain serialize")]
		public void OnLostFocus(int x, int y)
		{
			if (textBoxItem != null) textBoxItem.OnLostFocus(x, y);
			if (dateTimeItem != null) dateTimeItem.OnLostFocus(x, y);
		}

		[SuppressMessage("Microsoft.StyleCop.CSharp.LayoutRules", "SA1503:CurlyBracketsMustNotBeOmitted", Justification = "can contain serialize")]
		public void OnMouseDown(int x, int y, ref bool cancel)
		{
			if (textBoxItem != null) textBoxItem.OnMouseDown(x, y, ref cancel);
			if (dateTimeItem != null) dateTimeItem.OnMouseDown(x, y, ref cancel);
		}

		[SuppressMessage("Microsoft.StyleCop.CSharp.LayoutRules", "SA1503:CurlyBracketsMustNotBeOmitted", Justification = "can contain serialize")]
		public void OnMouseMove(int x, int y)
		{
			if (textBoxItem != null) textBoxItem.OnMouseMove(x, y);
			if (dateTimeItem != null) dateTimeItem.OnMouseMove(x, y);
		}

		[SuppressMessage("Microsoft.StyleCop.CSharp.LayoutRules", "SA1503:CurlyBracketsMustNotBeOmitted", Justification = "can contain serialize")]
		public void OnMouseUp(int x, int y)
		{
			if (textBoxItem != null) textBoxItem.OnMouseUp(x, y);
			if (dateTimeItem != null) dateTimeItem.OnMouseUp(x, y);
		}

		[SuppressMessage("Microsoft.StyleCop.CSharp.LayoutRules", "SA1503:CurlyBracketsMustNotBeOmitted", Justification = "can contain serialize")]
		public void OnRender(Graphics g)
		{
			if (textBoxItem != null) textBoxItem.OnRender(g);
			if (dateTimeItem != null) dateTimeItem.OnRender(g);
		}

		[SuppressMessage("Microsoft.StyleCop.CSharp.LayoutRules", "SA1503:CurlyBracketsMustNotBeOmitted", Justification = "can contain serialize")]
		public void OnResize()
		{
			if (textBoxItem != null) textBoxItem.OnResize();
			if (dateTimeItem != null) dateTimeItem.OnResize();
		}

		public event SenseListControl.RequestActionHandler RequestAction;

		public event SenseListControl.RequestParentHandler RequestParent;

		[SuppressMessage("Microsoft.StyleCop.CSharp.LayoutRules", "SA1503:CurlyBracketsMustNotBeOmitted", Justification = "can contain serialize")]
		public object Tag
		{
			get
			{
				if (textBoxItem != null) return textBoxItem.Tag;
				if (dateTimeItem != null) return dateTimeItem.Tag;
				return null;
			}
			set
			{
				if (textBoxItem != null) textBoxItem.Tag = value;
				if (dateTimeItem != null) dateTimeItem.Tag = value;
			}
		}

		[SuppressMessage("Microsoft.StyleCop.CSharp.LayoutRules", "SA1503:CurlyBracketsMustNotBeOmitted", Justification = "can contain serialize")]
		public bool Visible
		{
			get
			{
				if (textBoxItem != null) return textBoxItem.Visible;
				if (dateTimeItem != null) return dateTimeItem.Visible;
				return true;
			}
			set
			{
				if (textBoxItem != null) textBoxItem.Visible = value;
				if (dateTimeItem != null) dateTimeItem.Visible = value;
			}
		}

		[SuppressMessage("Microsoft.StyleCop.CSharp.LayoutRules", "SA1503:CurlyBracketsMustNotBeOmitted", Justification = "can contain serialize")]
		public int Width
		{
			get
			{
				if (textBoxItem != null) return textBoxItem.Width;
				if (dateTimeItem != null) return dateTimeItem.Width;
				return 0;
			}
			set
			{
				if (textBoxItem != null) textBoxItem.Width = value;
				if (dateTimeItem != null) dateTimeItem.Width = value;
			}
		}

		#endregion
	}
}
